package org.zjx.db;

import org.zjx.model.server.Server;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ServerDB {
    private static boolean isTableCreated = false;
    private final SQLiteHelper dbHelper;

    public ServerDB() {
        dbHelper = SQLiteHelper.getSingleton();
        if (!isTableCreated) {
            createTable();
            isTableCreated = true;
        }
    }

    public void createTable() {
        String createTable = "CREATE TABLE IF NOT EXISTS server (\n"
                + "    id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
                + "    mainName TEXT NOT NULL,\n"
                + "    mainLocation TEXT NOT NULL,\n"
                + "    subName TEXT NOT NULL,\n"
                + "    subLocation TEXT NOT NULL\n"
                + ");";
        dbHelper.createTable(createTable);
    }

    public void insert(Server server) {
        String sql = "INSERT INTO server(mainName, mainLocation, subName, subLocation) VALUES(?, ?, ?, ?)";
        Object[] params = {
                server.getMainName(),
                server.getMainLocation(),
                server.getSubName(),
                server.getSubLocation()
        };
        dbHelper.executeUpdate(sql, params);
    }

    public void update(Server server) {
        String sql = "UPDATE server SET mainName = ?, mainLocation = ?, subName = ?, subLocation = ? WHERE id = ?";
        Object[] params = {
                server.getMainName(),
                server.getMainLocation(),
                server.getSubName(),
                server.getSubLocation(),
                server.getId()
        };
        dbHelper.executeUpdate(sql, params);
    }

    public void deleteById(int id) {
        String sql = "DELETE FROM server WHERE id = ?";
        Object[] params = {id};
        dbHelper.executeUpdate(sql, params);
    }

    public Server findById(int id) {
        String sql = "SELECT * FROM server WHERE id = ?";
        Object[] params = {id};
        List<Server> servers = dbHelper.executeQuery(sql, params, Server.class);
        return servers.isEmpty() ? null : servers.get(0);
    }

    public List<Server> findByMainName(String mainName) {
        String sql = "SELECT * FROM server WHERE mainName = ?";
        Object[] params = {mainName};
        return dbHelper.executeQuery(sql, params, Server.class);
    }

    public List<Server> findBySubName(String subName) {
        String sql = "SELECT * FROM server WHERE subName = ?";
        Object[] params = {subName};
        return dbHelper.executeQuery(sql, params, Server.class);
    }

    public List<Server> findAll() {
        String sql = "SELECT * FROM server";
        return dbHelper.executeQuery(sql, new Object[]{}, Server.class);
    }

    public Map<String, Object> findAllWithPagination(int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        String sql = "SELECT * FROM server LIMIT ? OFFSET ?";
        List<Server> servers = dbHelper.executeQuery(sql, new Object[]{pageSize, offset}, Server.class);

        String countSql = "SELECT COUNT(*) FROM server";
        int totalRecords = dbHelper.executeQuery(countSql, new Object[]{}, Integer.class)
                .stream().findFirst().orElse(0);

        int totalPages = (int) Math.ceil((double) totalRecords / pageSize);

        Map<String, Object> result = new HashMap<>();
        result.put("serverList", servers);
        result.put("totalRecords", totalRecords);
        result.put("totalPages", totalPages);
        result.put("currentPage", pageNum);

        return result;
    }

    public void clearData() {
        dbHelper.clearData("server");
    }

    public void dropTable() {
        dbHelper.dropTable("server");
    }
}